{% macro render_field(field) -%}{#
 Macro for the output of a single field entry such as:

 int16u identifyTime = 0;
 optional int16u transitionTime = 3;
 optional nullable int16u transitionTime = 2;
 optional ExtensionFieldSet extensionFieldSets[] = 5;
#}

{%- if field.qualities %}{{field.qualities | idltxt}} {% endif -%}
{{field.data_type.name}}
{%- if field.data_type.max_length -%} <{{field.data_type.max_length}}> {%- endif -%}
{##} {{field.name}}
{%- if field.is_list %}[]{% endif -%}
{##} = {{field.code}};
{%- endmacro -%}

{% macro render_struct(s) -%}{#
 Macro for the output of a complete struct
#}
{%- if s.is_shared%}shared {% endif -%}
{%- if s.tag %}{{s.tag | idltxt}} {% endif -%}
{% if s.qualities %}{{s.qualities | idltxt}} {% endif -%}
struct {{s.name}} {##}
{%- if s.code is not none %}= {{s.code}} {% endif -%}
{
  {% for field in s.fields %}
  {{render_field(field)}}
  {% endfor %}
}
{%- endmacro -%}

{% macro specification_name_clarification(s) -%}{#
 Macro to add [spec_name="..."] if needed
#}
{%- if s.specification_name -%}
{##} [spec_name = "{{s.specification_name}}"]
{%- endif -%}
{%- endmacro -%}


// This IDL was auto-generated from a parsed data structure

{% for enum in idl.global_enums %}
enum {{enum.name}} : {{ enum.base_type}} {
  {% for entry in enum.entries %}
  {{entry.name}} = {{entry.code}} {{-specification_name_clarification(entry)}};
  {% endfor %}
}

{% endfor %}

{%- for bitmap in idl.global_bitmaps %}
bitmap {{bitmap.name}} : {{ bitmap.base_type}} {
  {% for entry in bitmap.entries %}
  {{entry.name}} = 0x{{"%X" | format(entry.code)}} {{-specification_name_clarification(entry)}};
  {% endfor %}
}

{% endfor %}

{%- for s in idl.global_structs %}
{{render_struct(s)}}

{% endfor %}

{%- for cluster in idl.clusters %}
{% if cluster.description %}/** {{cluster.description}} */
{% endif %}
{{cluster.api_maturity | idltxt}}cluster {{cluster.name}} = {{cluster.code}} {
  revision {{cluster.revision}};

  {% for enum in cluster.enums | selectattr("is_global")%}
  /* GLOBAL:
  enum {{enum.name}} : {{ enum.base_type}} {
    {% for entry in enum.entries %}
    {{entry.name}} = {{entry.code}} {{-specification_name_clarification(entry)}};
    {% endfor %}
  }
  */

  {% endfor %}

  {%- for bitmap in cluster.bitmaps | selectattr("is_global")%}
  /* GLOBAL:
  bitmap {{bitmap.name}} : {{ bitmap.base_type}} {
    {% for entry in bitmap.entries %}
    {{entry.name}} = 0x{{"%X" | format(entry.code)}} {{-specification_name_clarification(entry)}};
    {% endfor %}
  }
  */

  {% endfor %}

  {%- for s in cluster.structs | selectattr("is_global") %}
  /* GLOBAL:
  {{render_struct(s) | indent(2)}}
   */

  {% endfor %}

  {%- for enum in cluster.enums | rejectattr("is_global")%}
  {%+ if enum.is_shared%}shared {% endif -%}
  enum {{enum.name}} : {{ enum.base_type}} {
    {% for entry in enum.entries %}
    {{entry.name}} = {{entry.code}} {{-specification_name_clarification(entry)}};
    {% endfor %}
  }

  {% endfor %}

  {%- for bitmap in cluster.bitmaps | rejectattr("is_global")%}
  {%+ if bitmap.is_shared%}shared {% endif -%}
  bitmap {{bitmap.name}} : {{ bitmap.base_type}} {
    {% for entry in bitmap.entries %}
    {{entry.name}} = 0x{{"%X" | format(entry.code)}} {{-specification_name_clarification(entry)}};
    {% endfor %}
  }

  {% endfor %}

  {%- for s in cluster.structs | rejectattr("tag") | rejectattr("is_global") %}
  {{render_struct(s) | indent(2)}}

  {% endfor %}

  {%- for e in cluster.events %}
  {##}  {##}{% if e.qualities %}{{e.qualities | idltxt}} {% endif -%}
  {{e.api_maturity|idltxt}}{{e.priority | idltxt}} event {{e | event_access}}{{e.name}} = {{e.code}} {
    {% for field in e.fields %}
    {{render_field(field)}}
    {% endfor %}
  }

  {% endfor %}

  {%- for a in cluster.attributes %}
  {{a.api_maturity|idltxt}}{{a.qualities | idltxt}}attribute {{a | attribute_access}}{{render_field(a.definition)}}
  {% endfor %}

  {%- for s in cluster.structs | selectattr("tag") %}

  {{render_struct(s) | indent(2)}}
  {% endfor %}

  {%- for c in cluster.commands %}
  {% if loop.first %}

  {% endif %}
  {% if c.description %}
  /** {{c.description}} */
  {% endif %}
  {{c.qualities | idltxt}}command {{c | command_access}}{{c.name}}(
     {%- if c.input_param %}{{c.input_param}}{% endif -%}
  ): {{c.output_param}} = {{c.code}};
  {% endfor %}
}

{% endfor %}

{%- if idl.endpoints %}
{%- for endpoint in idl.endpoints %}
endpoint {{endpoint.number}} {
  {% for t in endpoint.device_types %}
  device type {{t.name}} = {{t.code}}, version {{t.version}};
  {% endfor%}

  {%-for b in endpoint.client_bindings %}
  {% if loop.first %}

  {% endif %}
  binding cluster {{b}};
  {% endfor %}

  {%-for c in endpoint.server_clusters %}

  server cluster {{c.name}} {
    {% for e in c.events_emitted %}
    emits event {{e}};
    {% if loop.last %}

    {% endif %}
    {% endfor %}
    {% for a in c.attributes %}
    {{"%-8s" | format(a.storage|idltxt) }} attribute {{a.name}}
    {%- if a.default is not none %} default = {{a.default|render_default}} {%- endif %};
    {% endfor %}
    {% for cmd in c.commands %}
    {% if loop.first %}

    {% endif %}
    handle command {{cmd.name}};
    {% endfor %}
  }
  {% endfor %}

}
{% if not loop.last %}

{% endif %}
{%  endfor %}
{%  endif %}
